package com.huika.dealontop.control.shops.activity;

import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.anim.berzier.BezierTravelAnim;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.extend.ActionBarHelper;
import com.huika.dealontop.control.base.extend.BaseActionBarActivity;
import com.huika.dealontop.control.nearby.activity.GMapActivity;
import com.huika.dealontop.control.shops.adapter.CommAdapter;
import com.huika.dealontop.control.shops.holder.ProductDetailHeaderHolder;
import com.huika.dealontop.control.timelimit.activity.MemberEvaluateActicvity;
import com.huika.dealontop.support.domain.OtherProductItemBean;
import com.huika.dealontop.support.domain.ProductDetailsBean;
import com.huika.dealontop.support.domain.ProductEvaluationBean;
import com.huika.dealontop.support.domain.ProductEvaluationItemBean;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.support.http.rsa.Base64Helper;
import com.huika.dealontop.utils.DebugTool;
import com.huika.dealontop.utils.FileTool;
import com.huika.dealontop.utils.SharpHelper;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.ToastMsg;
import com.huika.dealontop.views.webview.CustomWebView;
import com.huika.huikalib.utils.MoneyShowTool;
import com.huika.sharpsdklib.Utils;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.List;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/11/20 12:05 <br/>
 * Description: 商品详情
 */
public class ProductDetailsActivity2 extends BaseActionBarActivity implements Response.Listener<RequestResult<ProductDetailsBean>>,ProductDetailHeaderHolder.OnPageChangedListener, AbsListView.OnScrollListener, AdapterView.OnItemClickListener, ProductDetailHeaderHolder.OnPackageChangedListener {
    public static final String PRODUCT_ID = "ProductId";
    private String mProductId;

    private final int pageSize = 10;

    private ProductDetailHeaderHolder mHeaderHolder;

    private ProductDetailsBean mProductDetails;

    private PullToRefreshListView mLvContent;
    private RelativeLayout mRlFloatBuy;
    private TextView mTvPrice;
    private TextView mTvDiscount;
    private CommAdapter mCommAdapter;
    private boolean needToLoadMoreProduct;
    private boolean needToLoadMoreEvaluation;
    private SharpHelper sharpHelper;
    private CustomWebView mWebView;

    private View mHeadView;
    private View mFloatView;
    private View mIndicatorView;

    private ImageView mIvBackToTop;
    private ImageView mIvCart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if(intent != null){
            mProductId = intent.getStringExtra(PRODUCT_ID);
        }
        super.initData();
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_product_detail);
    }

    @Override
    protected void initWidget() {
        overridePendingTransition(R.anim.activity_right_in,
                R.anim.activity_right_out);
        ActionBarHelper actionBarHelper = getActionBarHelper();
        actionBarHelper.setTitleMsg(R.string.product_detail_title);
        actionBarHelper.setRightImg(R.mipmap.icon_sharp);
        actionBarHelper.setOnRightImgClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //分享
                if (sharpHelper == null) {
                    String title = null;
                    String price = null;
                    String shareCode = GlobalApp.getInstance().getUserReferralCode();
                    String productId = null;
                    if (mProductDetails != null && mProductDetails.getProduct() != null) {
                        ProductDetailsBean.ProductBean productBean = mProductDetails.getProduct();
                        title = productBean.getProductName();
                        price = MoneyShowTool.nolastDF(productBean.getSalePrice());
                        productId = String.valueOf(productBean.getProductId());
                    }
                    if (TextUtils.isEmpty(price) || TextUtils.isEmpty(productId)) {
                        return;
                    }

                    String url = getShareUrl(shareCode, productId);
                    String content = getSharpContent(title, price, url);
                    sharpHelper = new SharpHelper(ProductDetailsActivity2.this,
                            title,
                            url,
                            content,
                            null);
                    sharpHelper.setOnClickSharpIconListener(new SharpHelper.OnClickSharpIconListener() {
                        @Override
                        public void onClickSharp(View view) {
//                            if (view.getId() == R.id.iv_sharp_copy) {
//                                ClipboardHelper.copy(content, UIUtils.getContext());
//                                ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.copy_success));
//                            } else
                            if (view.getId() == R.id.iv_sharp_cut) {
                                try {
                                    String path = Utils.saveViewToImage(mRootView);
                                    FileTool.refreshAlbum(ProductDetailsActivity2.this, path);
                                    Toast.makeText(UIUtils.getContext(), UIUtils.getString(R.string.save_image_to_album), Toast.LENGTH_LONG).show();
                                } catch (Throwable throwable) {
                                    ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.cut_screen_failure));
                                    throwable.printStackTrace();
                                }
                            }
                        }
                    });
                }
                sharpHelper.show();
            }
        });

        mLvContent = (PullToRefreshListView) findViewById(R.id.lv_product_detail_content);
        mLvContent.setMode(PullToRefreshListView.Mode.DISABLED);
        mRlFloatBuy = (RelativeLayout) findViewById(R.id.rl_float_buy);
        mTvPrice = (TextView) findViewById(R.id.tv_product_detail_float_price);
        mTvDiscount = (TextView) findViewById(R.id.tv_product_detail_float_discount);
        mIvBackToTop = (ImageView) findViewById(R.id.iv_back_to_top);
        mIvBackToTop.setOnClickListener(this);
        mIvCart = (ImageView) findViewById(R.id.iv_product_cart);
        mIvCart.setOnClickListener(this);
        findViewById(R.id.btn_product_detail_float_buy).setOnClickListener(this);
        findViewById(R.id.tv_product_detail_buy).setOnClickListener(this);
        findViewById(R.id.tv_product_detail_add_to_cart).setOnClickListener(this);
        //显示头部数据
        mHeaderHolder = new ProductDetailHeaderHolder(this);
        mHeadView = mHeaderHolder.getRootView();
        mFloatView = mHeadView.findViewById(R.id.ll_buy);
        mIndicatorView = mHeadView.findViewById(R.id.rl_indicator);
        mLvContent.getRefreshableView().addHeaderView(mHeadView);
        mLvContent.setOnRefreshListener(new OnRefreshListener());
        mCommAdapter = new CommAdapter(this);
        mLvContent.setAdapter(mCommAdapter);

        mLvContent.setOnScrollListener(this);
        mLvContent.setOnItemClickListener(this);

        startLoadProductDetailData();
    }


    /**
     * 商品 或 评论 条目被点击
     * @param adapterView
     * @param view
     * @param i
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        DebugTool.d("", "item : " + i);
        Object item = mCommAdapter.getItem(i - 2); // i - 2 （pullToRefreshHead + 商品详情信息head）
        if(item instanceof OtherProductItemBean){
            OtherProductItemBean productItem = (OtherProductItemBean) item;
            // 打开商品详情
            Intent intent = new Intent(this, ProductDetailsActivity2.class);
            intent.putExtra(ProductDetailsActivity2.PRODUCT_ID, productItem.getProductId());
            startActivity(intent);

        }else if(item instanceof ProductEvaluationItemBean){
            //打开评论详情
            ProductEvaluationItemBean evaluationItem = (ProductEvaluationItemBean) item;
            Intent intent = new Intent(this, MemberEvaluateActicvity.class);
            intent.putExtra(Constants.EVALUATE_ID, evaluationItem.getEvaluateID());
            startActivity(intent);
        }
    }

    /**
     * 当套餐改变时
     * @param packageSelectBean
     * @param packageIndex
     */
    @Override
    public void onPackageChanged(ProductDetailsBean.PackageSelectBean packageSelectBean, int packageIndex) {
        if(packageSelectBean != null){
            mTvPrice.setText(MoneyShowTool.nolastDF(packageSelectBean.getSkuHKPrice()));
            if(packageSelectBean.getDiscount() > 0) {
                mTvDiscount.setText(MoneyShowTool.nolastDF(packageSelectBean.getDiscount(), false) + "% Off");
            }
        }
    }

    class OnRefreshListener implements PullToRefreshBase.OnRefreshListener2 {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {}
        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            loadMoreData();
        }
    }

    public void widgetClick(View v) {
        if((v.getId() == R.id.btn_product_detail_float_buy || v.getId() == R.id.tv_product_detail_buy)
                && mProductDetails != null){
            ProductDetailsBean.ProductBean productBean;
            if (mProductDetails == null || (productBean = mProductDetails.getProduct()) == null) {
                return;
            }
            ProductDetailsBean.PackageSelectBean packageSelectBean = mHeaderHolder.getPackage();
            if(packageSelectBean == null || packageSelectBean.getStock() < 1){
                ToastMsg.showToastMsg(this, UIUtils.getString(R.string.under_stock));
                return;
            }

            Intent intent = new Intent(this, SubmitOrderActivity.class);
            intent.putExtra(GMapActivity.MERCHANTNAME,productBean.getProductName());

            intent.putExtra(UseBonusActivity.CITYID, productBean.getCityId());
            if(packageSelectBean != null){
                intent.putExtra(SubmitOrderActivity.PRODUCT_SKUID, packageSelectBean.getSKU_ProducId());
                intent.putExtra(SubmitOrderActivity.PRODUCT_SKUNAME,packageSelectBean.getSkuName());
                intent.putExtra(SubmitOrderActivity.PRODUCT_STOCK,packageSelectBean.getStock());
                intent.putExtra(SubmitOrderActivity.SKUPRICE, packageSelectBean.getSkuHKPrice());
            }
            showActivity(this, intent);
        }
        else if(v.getId() == R.id.tv_product_detail_add_to_cart){
            ToastMsg.showToastMsg(this.getBaseContext(),"添加到购物车");
            addToCart();
        }
        else if(v.getId() == R.id.iv_back_to_top){
            if(mLvContent != null){
                ListView mlist = mLvContent.getRefreshableView();
                if (!(mlist).isStackFromBottom()) {
                    mlist.setStackFromBottom(true);
                }
                mlist.setStackFromBottom(false);
            }
        }
        super.widgetClick(v);
    }

    private void addToCart() {
        playAddToCartAnim();
    }

    private void playAddToCartAnim() {
        int[] location = new int[2];
        mLvContent.getLocationOnScreen(location);
        PointF startP = new PointF();
        startP.x = location[0];
        startP.y = location[1];

        mIvCart.getLocationOnScreen(location);
        PointF endP = new PointF();
        endP.x = location[0] - UIUtils.getDisplayMetrics().widthPixels / 2 + UIUtils.dip2px(15);
        endP.y = location[1];

        BezierTravelAnim travelAnim = new BezierTravelAnim.Builder(this)
                .setAnimView(View.inflate(getBaseContext(), R.layout.cart_anim, null))
                .setStartPointF(startP)
                .setEndPointF(endP)
                .setDuration(800)
                .build();

        travelAnim.startAnimation();
    }

    /**
     * ProductDetailHeaderHolder 里选项卡按钮切换后回调
     * @param page
     */
    @Override
    public void onPageChanged(int page) {
        mLvContent.onRefreshComplete();
        if (ProductDetailHeaderHolder.PRODUCT_PAGE == page) {
            mCommAdapter.setType(CommAdapter.PRODUCT);
            if (mCommAdapter.getCount() == 0) {
                startLoadProductData();
            }else{
                mCommAdapter.notifyDataSetChanged();
            }

            mLvContent.setMode(needToLoadMoreProduct ? PullToRefreshBase.Mode.PULL_UP_TO_REFRESH : PullToRefreshBase.Mode.DISABLED);
        } else if (ProductDetailHeaderHolder.COMMENTS_PAGE == page) {
            mCommAdapter.setType(CommAdapter.EVALUATION);
            if (mCommAdapter.getCount() == 0) {
                startLoadEvaluationData();
            }else{
                mCommAdapter.notifyDataSetChanged();
            }
            mLvContent.setMode(needToLoadMoreEvaluation ? PullToRefreshBase.Mode.PULL_UP_TO_REFRESH : PullToRefreshBase.Mode.DISABLED);
        }else {
            mCommAdapter.setType(CommAdapter.NOTES);
            mCommAdapter.notifyDataSetChanged();
            mLvContent.setMode(PullToRefreshBase.Mode.DISABLED);
        }


        /*int scale = 0;
        if (mLvContent.getFirstVisiblePosition() == 0) {
            View v = mLvContent.getChildAt(0);
            if (v != null) {
                scale = 0 - v.getTop();
            }
        }
        showLoadingDialog("");
        if (ProductDetailHeaderHolder.PRODUCT_PAGE == page) {
            if (mProductAdapter.getCount() == 0) {
                startLoadProductData();
            }
            mLvContent.setAdapter(mProductAdapter);

        } else if (ProductDetailHeaderHolder.COMMENTS_PAGE == page) {
            if (mEvaluationAdapter.getCount() == 0) {
                startLoadEvaluationData();
            }
            mLvContent.setAdapter(mEvaluationAdapter);
        } else if (ProductDetailHeaderHolder.NOTICE_PAGE == page) {
            mLvContent.setAdapter(emptyAdapter);
        }

        final int finalScale = scale;
        mLvContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mLvContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mLvContent.smoothScrollBy(finalScale, 0);
                UIUtils.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoadingDialog();
                    }
                },500);

            }
        });*/

    }

    /**
     * 加载第一页团购数据
     */
    private void startLoadProductData() {
        JSONWrapAjaxParams ajaxParams = getProductListParams();
        ajaxParams.putCommonTypeParam("Page", 1);
        FormResultRequest<List<OtherProductItemBean>> otherGrouponRequest = new FormResultRequest(UrlConstants.HOST + "Product/OtherGrouponList",
                new Response.Listener<RequestResult<List<OtherProductItemBean>>>() {
                    @Override
                    public void onResponse(RequestResult<List<OtherProductItemBean>> requestResult) {
                        dismissLoadingDialog();
                        if(requestResult != null && requestResult.flag == 1 && requestResult.getRs() != null){
//                            mProductAdapter.setGroup(requestResult.getRs());
                            mCommAdapter.setProductList(requestResult.getRs());
                            needToLoadMoreProduct = (requestResult.getRs().size() >= pageSize);
                            mLvContent.setMode(needToLoadMoreProduct ? PullToRefreshBase.Mode.PULL_UP_TO_REFRESH : PullToRefreshBase.Mode.DISABLED);
                        }
                    }
                }, this,
                new TypeToken<RequestResult<List<OtherProductItemBean>>>() {
                }.getType());
        otherGrouponRequest.setRequestParams(ajaxParams);
        showLoadingDialog("");
        executeRequest(otherGrouponRequest);
    }

    /**
     * 加载更多团购数据
     */
    private void loadMoreProductData() {
        JSONWrapAjaxParams ajaxParams = getProductListParams();
        int pageNo = getProductPageNumber();
        DebugTool.d("onScrollStateChanged", "pageNo : " + pageNo);
        ajaxParams.putCommonTypeParam("Page", getProductPageNumber() + 1);
        FormResultRequest<List<OtherProductItemBean>> otherGrouponRequest = new FormResultRequest(UrlConstants.HOST + "Product/OtherGrouponList",
                new Response.Listener<RequestResult<List<OtherProductItemBean>>>() {
                    @Override
                    public void onResponse(RequestResult<List<OtherProductItemBean>> requestResult) {
                        if(mCommAdapter.getType() == CommAdapter.PRODUCT)
                            mLvContent.onRefreshComplete();
                        if(requestResult != null && requestResult.flag == 1 && requestResult.getRs() != null){
//                            mProductAdapter.setGroup(requestResult.getRs());
                            mCommAdapter.addProductList(requestResult.getRs());

                            needToLoadMoreProduct = (requestResult.getRs().size() >= pageSize);
                            mLvContent.setMode(needToLoadMoreProduct ? PullToRefreshBase.Mode.PULL_UP_TO_REFRESH : PullToRefreshBase.Mode.DISABLED);
                        }
                    }
                }, this,
                new TypeToken<RequestResult<List<OtherProductItemBean>>>() {
                }.getType());
        otherGrouponRequest.setRequestParams(ajaxParams);
        executeRequest(otherGrouponRequest);
    }

    /**
     * 加载第一页评论数据
     */
    private void startLoadEvaluationData() {
        JSONWrapAjaxParams ajaxParams = getEvaluationListParams();
        ajaxParams.putCommonTypeParam("pageNo", 1);
        FormResultRequest otherGrouponRequest = new FormResultRequest(UrlConstants.HOST + "Product/UserEvaluation",
                new Response.Listener<RequestResult<ProductEvaluationBean>>() {
                    @Override
                    public void onResponse(RequestResult<ProductEvaluationBean> requestResult) {
                        dismissLoadingDialog();
                        if(requestResult != null && requestResult.flag == 1 && requestResult.getRs() != null){
//                            mEvaluationAdapter.setGroup(requestResult.getRs());
                            List<ProductEvaluationItemBean> evaluationList = requestResult.getRs().getData();
                            mCommAdapter.setEvaluationList(evaluationList);
                            if(evaluationList != null){
                                needToLoadMoreEvaluation = (evaluationList.size() >= pageSize);
                            }else {
                                needToLoadMoreEvaluation = false;
                            }
                            mLvContent.setMode(needToLoadMoreEvaluation ? PullToRefreshBase.Mode.PULL_UP_TO_REFRESH : PullToRefreshBase.Mode.DISABLED);
                        }
                    }
                }, this,
                new TypeToken<RequestResult<ProductEvaluationBean>>() {
                }.getType());
        otherGrouponRequest.setRequestParams(ajaxParams);
        showLoadingDialog("");
        executeRequest(otherGrouponRequest);
    }



    /**
     * 加载更多评论数据
     */
    private void loadMoreEvaluationData() {
        JSONWrapAjaxParams ajaxParams = getEvaluationListParams();
        int pageNo = getEvaluationPageNumber();
        ajaxParams.putCommonTypeParam("pageNo", getEvaluationPageNumber() + 1);
        FormResultRequest otherGrouponRequest = new FormResultRequest(UrlConstants.HOST + "Product/UserEvaluation",
                new Response.Listener<RequestResult<ProductEvaluationBean>>() {
                    @Override
                    public void onResponse(RequestResult<ProductEvaluationBean> requestResult) {
                        mLvContent.onRefreshComplete();
                        if(requestResult != null && requestResult.flag == 1 && requestResult.getRs() != null){
//                            mEvaluationAdapter.setGroup(requestResult.getRs());
                            List<ProductEvaluationItemBean> evaluationList = requestResult.getRs().getData();
                            mCommAdapter.addEvaluationList(evaluationList);
                            if(evaluationList != null){
                                needToLoadMoreEvaluation = (evaluationList.size() >= pageSize);
                            }else {
                                needToLoadMoreEvaluation = false;
                            }
                            mLvContent.setMode(needToLoadMoreProduct ? PullToRefreshBase.Mode.PULL_UP_TO_REFRESH : PullToRefreshBase.Mode.DISABLED);
                        }
                    }
                }, this,
                new TypeToken<RequestResult<ProductEvaluationBean>>() {
                }.getType());
        otherGrouponRequest.setRequestParams(ajaxParams);
        executeRequest(otherGrouponRequest);
    }

    private int getProductPageNumber() {
        return (mCommAdapter.getProductCount() + (pageSize - 1))  / pageSize;
    }

    private int getEvaluationPageNumber() {
        return (mCommAdapter.getEvaluationCount() + (pageSize - 1))  / pageSize;
    }

    private JSONWrapAjaxParams productListParams;
    private JSONWrapAjaxParams getProductListParams() {
        if(productListParams == null){
            productListParams = new JSONWrapAjaxParams();
            productListParams.putCommonTypeParam("MerchantID", mProductDetails.getProduct().getMerchantID());
            productListParams.putCommonTypeParam("ProductId",mProductDetails.getProduct().getProductId());
            productListParams.putCommonTypeParam("PageSize",pageSize);
        }
        return productListParams;
    }
    private JSONWrapAjaxParams evaluationListParams;
    private JSONWrapAjaxParams getEvaluationListParams() {
        if(evaluationListParams == null){
            evaluationListParams = new JSONWrapAjaxParams();
            evaluationListParams.putCommonTypeParam("productID", mProductDetails.getProduct().getProductId());
//            evaluationListParams.putCommonTypeParam("productID", "FWaSp7MkdmB1A0/hcdWhisldsaZuBlRiCemVyoZnn+/QldnJBAJCPTrNkbv38slfFd/i35LtdkbCxdiZn4SqyrEdkm4CDPAh00I65Dm2dACQ9PO8fRnOCa3k0KaEgvAFaJf0TUDJbhQnxETR0GMCCHzhLTofjy0721GvzJm5ikM=");
            evaluationListParams.putCommonTypeParam("MerchantID", mProductDetails.getProduct().getMerchantID());
//            evaluationListParams.putCommonTypeParam("MerchantID", "");
            evaluationListParams.putCommonTypeParam("type", 1);
            evaluationListParams.putCommonTypeParam("pageSize",pageSize);
        }
        return evaluationListParams;
    }


    private void startLoadProductDetailData() {
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("ProductId", mProductId);
        ajaxParams.putCommonTypeParam("Lang", UIUtils.getString(R.string.common_lang));
        FormResultRequest<ProductDetailsBean> loginRequest = new FormResultRequest(UrlConstants.PRODUCT_DETAIL, this, this,
                new TypeToken<RequestResult<ProductDetailsBean>>() {
                }.getType());
        loginRequest.setRequestParams(ajaxParams);
        showLoadingDialog("");
        executeRequest(loginRequest);

    }

    /**
     * 商品详情数据相应
     * @param requestResult
     */
    @Override
    public void onResponse(RequestResult<ProductDetailsBean> requestResult) {
        mLvContent.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH);
        dismissLoadingDialog();
        if(requestResult != null && (mProductDetails = requestResult.getRs()) != null){
            //排序
            sortPackageList();

            //刷新数据
            mHeaderHolder.setData(mProductDetails);
            //设置页面切换监听回调
            mHeaderHolder.setOnPageChangedListener(this);
            mHeaderHolder.setOnPackageChangedListener(this);

            //刷新浮动条信息
            if(mProductDetails.getProduct() != null){
                mTvPrice.setText(MoneyShowTool.nolastDF(mProductDetails.getProduct().getSalePrice()));
                mTvDiscount.setText(MoneyShowTool.nolastDF(mProductDetails.getProduct().getDiscount(), false) + "% Off");
            }
        }

    }

    private void sortPackageList() {
        if(mProductDetails.getPackageSelect() != null
                && mProductDetails.getPackageSelect().size() > 0){
            Collections.sort(mProductDetails.getPackageSelect());

            //如果有套餐 就 显示第一个套餐的价格 和折扣
            ProductDetailsBean.PackageSelectBean  packageItem = mProductDetails.getPackageSelect().get(0);
            if(mProductDetails.getProduct() != null){
                mProductDetails.getProduct().setSalePrice(packageItem.getSkuHKPrice());
                mProductDetails.getProduct().setDiscount(packageItem.getDiscount());
            }
        }
    }

    /**
     * ListView 滑动监听 控制浮动条显示、隐藏
     * @param absListView
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
//        if(AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState){
//            int position = mLvContent.getRefreshableView().getLastVisiblePosition();
//            int count = mCommAdapter.getCount();
//            DebugTool.d("onScrollStateChanged", "position : " + position);
//            DebugTool.d("onScrollStateChanged", "count : " + count);
//            if(position > 1 && count == position - 1){
//                loadMoreData();
//            }
//        }
    }

    private void loadMoreData() {
        if(mCommAdapter.getType() == CommAdapter.PRODUCT){
            if(needToLoadMoreProduct){
                loadMoreProductData();
            }
        }else if(mCommAdapter.getType() == CommAdapter.EVALUATION){
            if(needToLoadMoreEvaluation){
                loadMoreEvaluationData();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem > 1) {
            mRlFloatBuy.setVisibility(View.VISIBLE);
            mIvBackToTop.setVisibility(View.VISIBLE);
        } else {
            if (mHeadView != null && (mHeadView.getTop() + mFloatView.getTop()) <= 0) {
                mRlFloatBuy.setVisibility(View.VISIBLE);
            }else{
                mRlFloatBuy.setVisibility(View.GONE);
            }

            if (mHeadView != null && (mHeadView.getTop() + mIndicatorView.getTop()) <= 0) {
                mIvBackToTop.setVisibility(View.VISIBLE);
            }else{
                mIvBackToTop.setVisibility(View.GONE);
            }
        }

    }


    private String getSharpContent(String title, String price, String url){
        return UIUtils.getFormatString(R.string.sharp_content,title,price,url);
    }

    private String getShareUrl(String shareCode, String productId) {
        String url = null;
        try {
            url = UrlConstants.HOST + "html5/show.html?shareCode=" + shareCode + "&ProductId=" + Base64Helper.encode(productId.getBytes("utf-8")) + "&lang=" + UIUtils.getString(R.string.common_lang);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return url;
    }

}
